//
//  NSString+Tool.m
//  YYPaas
//
//  Created by 余意 on 2018/6/26.
//  Copyright © 2018年 余意. All rights reserved.
//

#import "NSString+Tool.h"

@implementation NSString (Tool)

+ (BOOL)isMobile:(NSString *)mobile
{
    NSString * MOBILE = @"^(13|14|15|16|17|18|19)\\d{9}$";
    NSPredicate * regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    return [regextestmobile evaluateWithObject:mobile];
}

+ (BOOL)isEmail:(NSString *)email
{
    NSString * EMAIL = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate * regextestemail = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", EMAIL];
    return  [regextestemail evaluateWithObject:email];
}

+ (BOOL)isIDCardNo:(NSString *)number
{
    //正则表达式
    NSString * ID = @"^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$";
    NSPredicate * regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", ID];
    BOOL isIDNum = [regextestmobile evaluateWithObject:number];
    if (!isIDNum)
    {
        return NO;
    }
    
    //11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江 ",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北 ",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏 ",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外
    NSArray * areasArray = @[@"11",@"12",@"13",@"14",@"15",
                             @"21",@"22",@"23",
                             @"31",@"32",@"33",@"34",@"35",@"36",@"37",
                             @"41",@"42",@"43",@"44",@"45",@"46",
                             @"50",@"51",@"52",@"53",@"54",
                             @"61",@"62",@"63",@"64",@"65",
                             @"71",
                             @"81",@"82",
                             @"91"];
    NSString * areaStartString = [number substringToIndex:2];
    isIDNum = NO;
    for (NSString * areaCode in areasArray)
    {
        if ([areaCode isEqualToString:areaStartString])
        {
            isIDNum = YES;
            break;
        }
    }
    
    if (!isIDNum)
    {
        return NO;
    }
    
    //加权因子
    NSArray * codeArray = @[@"7",@"9",@"10",@"5",@"8",@"4",@"2",@"1",@"6",@"3",@"7",@"9",@"10",@"5",@"8",@"4",@"2"];
    
    //校验字典
    NSDictionary * checkCodeDic = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"1",@"0",@"X",@"9",@"8",@"7",@"6",@"5",@"4",@"3",@"2", nil]  forKeys:[NSArray arrayWithObjects:@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10", nil]];
    
    NSScanner * scan = [NSScanner scannerWithString:[number substringToIndex:17]];
    int val;
    
    isIDNum = [scan scanInt:&val] && [scan isAtEnd];
    if (!isIDNum)
    {
        return NO;
    }
    
    int sumValue = 0 ;
    
    for (NSInteger i = 0 ; i < 17 ; i ++ )
    {
        sumValue += [[number substringWithRange:NSMakeRange(i , 1) ] intValue]* [[codeArray objectAtIndex:i] intValue];
    }
    
    NSString * strlast = [checkCodeDic objectForKey:[NSString stringWithFormat:@"%d",sumValue % 11]];
    
    if ([strlast isEqualToString: [[number substringWithRange:NSMakeRange(17, 1)]uppercaseString]]) {
        return YES;
    }
    
    return NO;
}

@end
